package rpc

import (
	"fmt"
	"go.uber.org/zap"
	"google.golang.org/grpc"
)

func NewRpcAppEr(domain, port string) *grpc.ClientConn {
	var rpc RpcAppEr = &service{
		domain: domain,
		port:   port,
	}
	conn, err := rpc.CreateApplication()
	if err != nil {
		// todo 写日志
		log := zap.NewExample()
		log.Error(fmt.Sprintf("this is error message + %s", err))
		panic(domain + port + "rpc connect error")
	}

	return conn
}

type RpcAppEr interface {
	CreateApplication() (*grpc.ClientConn, error)
}

var _ RpcAppEr = &service{}

type service struct {
	conn   *grpc.ClientConn
	domain string
	port   string
}

// CreateApplication 创建服务应用
func (s *service) CreateApplication() (*grpc.ClientConn, error) {
	fmt.Printf(fmt.Sprintf("%s%s", s.domain, s.port))
	// 监听下层微服务端口
	conn, err := grpc.Dial(
		fmt.Sprintf("%s%s", s.domain, s.port),
		grpc.WithInsecure(),
	)

	return conn, err
}
